const user = require("../sql/user")
module.exports = function(){
  return async (req,res,next)=>{
    const {sex, age, tel, hobby=""} = req.body;
    // 根据前端发送的信息，准备要修改的具体信息项
    const data = {};
    if(sex) data.sex = sex;
    if(age) data.age = age;
    if(tel) data.tel = tel;
    // 处理爱好数据，为数组
    const h = [];
    try {
      h.push(...JSON.parse(hobby))
    } catch (error) {
      h.push(...hobby.split(","))
    }
    if(h.length!==0) data.hobby = h;

    try{
      // 根据登录用户的id，修改自身信息
      await user.updateOne({
        userId: req.userMsg.userId
      }, data)
      res.send({state:0, msg:"修改成功"})
    }catch(error){
      res.send({state:0, msg:"修改失败", error})
    }
  }
}